package cn.com.gridinfo.sooc.core.course.discuss.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.discuss.activity.DiscussInfoActivity;
import cn.com.gridinfo.sooc.core.course.discuss.bean.Discuss;
import cn.com.gridinfo.sooc.utils.DateUtil;
import cn.com.gridinfo.sooc.utils.widget.textview.HtmlImageGetterUtil;

/**
 * 课程-讨论 adapter
 * Created by yingchunli on 2015/12/31.
 */
public class DiscussAdapter extends UltimateViewAdapter<DiscussAdapter.ViewHolder> {

    private List<Discuss> discusseList;
    private Context context;

    public DiscussAdapter(List<Discuss> discusseList,Context context) {
        this.discusseList = discusseList;
        this.context=context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.course_discuss_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        view.setOnClickListener(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= discusseList.size() : position < discusseList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            holder.tlnr.setText(discusseList.get(position).getTlnr());
            holder.hfnum.setText(discusseList.get(position).getHfts() + "");
            holder.author.setText(discusseList.get(position).getName());
            holder.time.setText(discusseList.get(position).getTime_show());
        }
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void loadmore(List<Discuss> list) {
        discusseList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Discuss> list) {
        discusseList.clear();
        discusseList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        return discusseList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener{
        TextView tlnr;//讨论内容
        TextView time;//时间
        TextView author;//发布人
        TextView hfnum;//回复个数
        TextView quanwen;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                tlnr = (TextView) itemView.findViewById(R.id.tlnr_discuss_item);
                author = (TextView) itemView.findViewById(R.id.author_discuss_item);
                time = (TextView) itemView.findViewById(R.id.time_discuss_item);
                hfnum = (TextView) itemView.findViewById(R.id.hfnum_discuss_item);
//                quanwen = (TextView) itemView.findViewById(R.id.tlnr_quanwen_item);
            }
        }

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(context, DiscussInfoActivity.class);
            intent.putExtra("tlid",discusseList.get(getAdapterPosition()).getTlid());
            intent.putExtra("hfts",discusseList.get(getAdapterPosition()).getHfts()+"");
            intent.putExtra("tlnr",discusseList.get(getAdapterPosition()).getTlnr());
            intent.putExtra("input_time",discusseList.get(getAdapterPosition()).getInput_time());
            intent.putExtra("input_name",discusseList.get(getAdapterPosition()).getName());
            context.startActivity(intent);
        }
    }
}